package example;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import my.com.sunway.bean.*;
import my.com.sunway.util.*;
import java.text.*;
import java.util.*;
import org.apache.log4j.*;

/**
 * @author Deepak Kumar
 *
 * http://www.roseindia.net
 * Hibernate example to inset data into Contact table
 */
public class SelectExample {
 static Logger logger = Logger.getLogger(SelectExample.class);

 public static void main(String[] args) {
  Session session = null;
  
  try{
   // This step will read hibernate.cfg.xml and prepare hibernate for use   
   session = HibernateUtil.currentSession();
   //System.out.println("Configuration = "+configuration);   
   //Create new instance of Contact and set values in it by reading them from form object
   logger.info("Selecting Record");
   java.util.Date date = new java.util.Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyDDD");
   String formatted_date = sdf.format(date);
   long parse_date = Long.parseLong(formatted_date)-1900001;

   Query query = session.getNamedQuery("my.com.sunway.query.F0101.getQueryByUPMJ");
   //query.setFirstResult(1);
   //query.setMaxResults(5);
   logger.info("formatted date = "+formatted_date);
   logger.info("day-of-year = "+parse_date);
   query.setLong("julian_date",parse_date);
   List results = query.list();
   displayProductsList(results);
   logger.info("Done");
  }catch(Exception e){
   System.out.println(e.getMessage());
  }finally{
   try{
    // Actual contact insertion will happen at this step
    session.flush();
    session.close();
   }catch(HibernateException ex){
    logger.error("[error] "+ex);
   }
  }
 }
 
 static public void displayProductsList(List list){
  Iterator iter = list.iterator();
  if (!iter.hasNext()){
   logger.info("No address book to display.");
   return;
  }
  for(int row=0; iter.hasNext()&&row<20; row++) { 
   F0101 addressbook = (F0101) iter.next();
   String msg = "";
   msg += "[no="+addressbook.getId();
   msg += "|name="+addressbook.getAbalph();
   msg += "|bu="+addressbook.getAbmcu();
   msg += "|created_date="+addressbook.getAbupmj()+"]";
   logger.info(msg);
  }
 }
}
